package Admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class Payment extends javax.swing.JDialog {

    private javax.swing.JButton btexit;
    private javax.swing.JButton btpayment;
    private javax.swing.JLabel lbstatus;
    private javax.swing.JTextField txtstatus;
    private javax.swing.JButton btupdatebill;

    private int dem, x1, x2, dem2;
    private String status;
    public static String getA1() {
        return a1;
    }

    public static void setA1(String a1) {
        Payment.a1 = a1;
    }

    public static String getA2() {
        return a2;
    }

    public static void setA2(String a2) {
        Payment.a2 = a2;
    }

    public static String getA3() {
        return a3;
    }

    public static void setA3(String a3) {
        Payment.a3 = a3;
    }

    public static String getA4() {
        return a4;
    }

    public static void setA4(String a4) {
        Payment.a4 = a4;
    }

    public static String getA5() {
        return a5;
    }

    public static void setA5(String a5) {
        Payment.a5 = a5;
    }

    public static String getA6() {
        return a6;
    }

    public static void setA6(String a6) {
        Payment.a6 = a6;
    }

    public static String getA7() {
        return a7;
    }

    public static void setA7(String a7) {
        Payment.a7 = a7;
    }

    public static String getA8() {
        return a8;
    }

    public static void setA8(String a8) {
        Payment.a8 = a8;
    }

    public static String getA9() {
        return a9;
    }

    public static void setA9(String a9) {
        Payment.a9 = a9;
    }

    public static String getA10() {
        return a10;
    }

    public static void setA10(String a10) {
        Payment.a10 = a10;
    }

    public static String getA11() {
        return a11;
    }

    public static void setA11(String a11) {
        Payment.a11 = a11;
    }

    public static String getA12() {
        return a12;
    }

    public static void setA12(String a12) {
        Payment.a12 = a12;
    }
    

    
    private javax.swing.JButton     btsubmit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel      lbcompanyname;
    private javax.swing.JTable      tbdulieu;
    private javax.swing.JTextField  txtcompanyname;
    ConnectRMI connectRMI = new ConnectRMI();
    private DefaultTableModel       dtm;
    private String                  click0, click1, click2, click3, click4, click5, click6, click7, click8, click9,
            click10, click11, click12, click13, click14,click15;
    private static String           a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13 ,a14,a15;
    private int                     row;

    public Payment(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void reload() {

        while (tbdulieu.getRowCount() != 0) {
            dtm.removeRow(0);
        }

        try {
            Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
            EtowInterface obj = (EtowInterface) registry.lookup(connectRMI.regis);

            List<GroupClass.BillClass> a = obj.bill();

            for (GroupClass.BillClass d : a) {
                String name = d.getName();
                String companyname = d.getCompanyname();
                String email    = d.getEmail();
                String unitname = d.getUnitname();
                String unitprice = d.getUnitprice();
                String power = d.getPower();
                String water = d.getWater();
                String security = d.getSecurity();
                String washing = d.getWashingroom();
                String clean = d.getCleanbuilding();
                String main = d.getMaintenance();
                String amountofparking = d.getAmoutofparking();
                String datapayment = ConverDateTime(d.getDatapayment());
                String status = d.getStatus();
                if (status.equals("0")) {
                    status = "No Payment";
                }
                else {
                    status = "Paid";
                }
                int billid = d.getBillid();               
                int billpayment = d.getBillpayment();
                
                dtm.addRow(new Object[] { name, companyname,email, unitname, unitprice, power, water, security, washing,
                            clean, main, amountofparking, datapayment,status, billid, billpayment });

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void Seach() {
        if (txtcompanyname.getText().length() > 0 && txtstatus.getText().length() == 0) {
            x1 = tbdulieu.getRowCount();
            dem = 0;
            for (int i = 0; i < x1; i++) {
                String status;
                if (tbdulieu.getValueAt(i, 1).toString().equals(txtcompanyname.getText())) {
                    dem += 1;
                    String name = tbdulieu.getValueAt(i, 0).toString();
                    String companyname = tbdulieu.getValueAt(i, 1).toString();
                    String email = tbdulieu.getValueAt(i, 2).toString();
                    String unitname = tbdulieu.getValueAt(i, 3).toString();
                    String unitprice = tbdulieu.getValueAt(i, 4).toString();
                    String power = tbdulieu.getValueAt(i, 5).toString();
                    String water = tbdulieu.getValueAt(i, 6).toString();
                    String security = tbdulieu.getValueAt(i, 7).toString();
                    String washing = tbdulieu.getValueAt(i, 8).toString();
                    String clean = tbdulieu.getValueAt(i, 9).toString();
                    String main = tbdulieu.getValueAt(i, 10).toString();
                    String amountofparking = tbdulieu.getValueAt(i, 11).toString();
                    String datapayment = tbdulieu.getValueAt(i, 12).toString();
                    if( tbdulieu.getValueAt(i, 13).toString().equals("No Payment"))
                    {
                       status = "No Payment";
                    }
                    else
                    {
                        status = "Paid";
                    }
                    
                    String billid = tbdulieu.getValueAt(i, 14).toString();
                    String billpayment = tbdulieu.getValueAt(i, 15).toString();

                    dtm.addRow(new Object[] { name, companyname,email, unitname, unitprice, power, water, security, washing,
                            clean, main, amountofparking, datapayment,status, billid, billpayment });
                }

            }

            for (int i = 0; i < tbdulieu.getRowCount(); i++) {
                while (tbdulieu.getRowCount() != dem) {
                    dtm.removeRow(0);
                }
            }
            txtcompanyname.setText("");
            txtstatus.setText("");
        } else if (txtstatus.getText().length() > 0 && txtcompanyname.getText().length() == 0) {
            x2 = tbdulieu.getRowCount();
            System.out.println(tbdulieu.getValueAt(1, 13).toString());
            dem2 = 0;
            String status ;
            for (int i = 0; i < x2; i++) {
                if (tbdulieu.getValueAt(i, 13).toString().equals(txtstatus.getText())) {
                    dem2 += 1;
                    String name = tbdulieu.getValueAt(i, 0).toString();
                    String companyname = tbdulieu.getValueAt(i, 1).toString();
                    String email = tbdulieu.getValueAt(i, 2).toString();
                    String unitname = tbdulieu.getValueAt(i, 3).toString();
                    String unitprice = tbdulieu.getValueAt(i, 4).toString();
                    String power = tbdulieu.getValueAt(i, 5).toString();
                    String water = tbdulieu.getValueAt(i, 6).toString();
                    String security = tbdulieu.getValueAt(i, 7).toString();
                    String washing = tbdulieu.getValueAt(i, 8).toString();
                    String clean = tbdulieu.getValueAt(i, 9).toString();
                    String main = tbdulieu.getValueAt(i, 10).toString();
                    String amountofparking = tbdulieu.getValueAt(i, 11).toString();
                    String datapayment = tbdulieu.getValueAt(i, 12).toString();
                    if( tbdulieu.getValueAt(i, 13).toString().equals("No Payment"))
                    {
                       status = "No Payment";
                    }
                    else
                    {
                        status = "Paid";
                    }
                    
                    String billid = tbdulieu.getValueAt(i, 14).toString();
                    String billpayment = tbdulieu.getValueAt(i, 15).toString();
                    
                    dtm.addRow(new Object[] { name, companyname,email, unitname, unitprice, power, water, security, washing,
                            clean, main, amountofparking, datapayment,status, billid, billpayment });
                }
            }
            for (int i = 0; i < tbdulieu.getRowCount(); i++) {
                while (tbdulieu.getRowCount() != dem2) {
                    dtm.removeRow(0);
                }
            }
            txtcompanyname.setText("");
            txtstatus.setText("");
        } else if (txtcompanyname.getText().length() > 0 && txtstatus.getText().length() > 0) {
            x1 = tbdulieu.getRowCount();
            dem = 0;
            for (int i = 0; i < x1; i++) {
                String status ;
                if (tbdulieu.getValueAt(i, 1).toString().equals(txtcompanyname.getText())
                        && tbdulieu.getValueAt(i, 13).toString().equals(txtstatus.getText())) {
                    dem += 1;
                    String name = tbdulieu.getValueAt(i, 0).toString();
                    String companyname = tbdulieu.getValueAt(i, 1).toString();
                    String email = tbdulieu.getValueAt(i, 2).toString();
                    String unitname = tbdulieu.getValueAt(i, 3).toString();
                    String unitprice = tbdulieu.getValueAt(i, 4).toString();
                    String power = tbdulieu.getValueAt(i, 5).toString();
                    String water = tbdulieu.getValueAt(i, 6).toString();
                    String security = tbdulieu.getValueAt(i, 7).toString();
                    String washing = tbdulieu.getValueAt(i, 8).toString();
                    String clean = tbdulieu.getValueAt(i, 9).toString();
                    String main = tbdulieu.getValueAt(i, 10).toString();
                    String amountofparking = tbdulieu.getValueAt(i, 11).toString();
                    String datapayment = tbdulieu.getValueAt(i, 12).toString();
                    if(tbdulieu.getValueAt(i, 13).toString().equals("No Payment"))
                    {
                       status = "No Payment";
                    }
                    else
                    {
                        status = "Paid";
                    }
                    
                    String billid = tbdulieu.getValueAt(i, 14).toString();
                    String billpayment = tbdulieu.getValueAt(i, 15).toString();
                    
                    dtm.addRow(new Object[] { name, companyname,email, unitname, unitprice, power, water, security, washing,
                            clean, main, amountofparking, datapayment,status, billid, billpayment });
                }
            }
            for (int i = 0; i < tbdulieu.getRowCount(); i++) {
                while (tbdulieu.getRowCount() != dem) {
                    dtm.removeRow(0);
                }
            }
            txtcompanyname.setText("");
            txtstatus.setText("");
        } else {
            reload();
        }
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {

        lbcompanyname = new javax.swing.JLabel();
        txtcompanyname = new javax.swing.JTextField();
        btexit = new javax.swing.JButton();
        btpayment = new javax.swing.JButton();
        btsubmit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbdulieu = new javax.swing.JTable();
        lbstatus = new javax.swing.JLabel();
        txtstatus = new javax.swing.JTextField();
        btupdatebill = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        
        lbstatus.setText("Status");
        lbcompanyname.setText("Companyname");

        btexit.setText("Exit");
        btexit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                setVisible(false);
            }
        });

        btpayment.setText("Payment");
        btpayment.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (a1 == null) {

                } else {
                    PDFBillPayment bp = new PDFBillPayment(null, true);
                    bp.setPaymentid(a12);

                    bp.setVisible(true);
                    while (bp.isVisible() != true) {
                        if (bp.getCheckepayment().equals("close")) {
                            reload();
                            break;
                        }
                    }
                }

            }
        });

        btsubmit.setText("Seach");
        btsubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Seach();

            }
        });
        
        btupdatebill.setText("Update Bill");
        btupdatebill.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
               UpdateBill ub = new UpdateBill(null, true);
               ub.setIdbuill(a14);
               ub.setIdbuillpayment(a15);
               ub.setVisible(true); 
            }
        });
        Object colNames[] = { "Name", "CompanyName","Email", "UnitName", "UnitPrice", "Power", "Water", "Security",
                "WashingRoom", "CleanBuildingGlasses", "MaintenanceElevator", "AmountOfParking", "DataOfPayment","Status", "",
                "" };
        Object[][] data = {};

        dtm = new DefaultTableModel(data, colNames);
        tbdulieu = new JTable(dtm);
        tbdulieu.getTableHeader().setReorderingAllowed(false);
        tbdulieu.getColumnModel().getColumn(14).setMaxWidth(0);
        tbdulieu.getColumnModel().getColumn(14).setMinWidth(0);
        tbdulieu.getColumnModel().getColumn(14).setWidth(0);
        tbdulieu.getColumnModel().getColumn(15).setMaxWidth(0);
        tbdulieu.getColumnModel().getColumn(15).setMinWidth(0);
        tbdulieu.getColumnModel().getColumn(15).setWidth(0);
        try {
            Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
            EtowInterface obj = (EtowInterface) registry.lookup(connectRMI.regis);

            List<GroupClass.BillClass> a = obj.bill();

            for (GroupClass.BillClass d : a) {
                String name = d.getName();
                String companyname = d.getCompanyname();
                String email    = d.getEmail();
                String unitname = d.getUnitname();          
                String unitprice = d.getUnitprice();
                String power = d.getPower();
                String water = d.getWater();
                String security = d.getSecurity();
                String washing = d.getWashingroom();
                String clean = d.getCleanbuilding();
                String main = d.getMaintenance();
                String amountofparking = d.getAmoutofparking();
                String datapayment = ConverDateTime(d.getDatapayment());
                String status = d.getStatus();
                if (status.equals("0")) {
                    status = "No Payment";
                }
                else {
                    status = "Paid";
                }
                int billid = d.getBillid();
                int billpayment = d.getBillpayment();

                    dtm.addRow(new Object[] { name, companyname,email, unitname, unitprice, power, water, security, washing,
                            clean, main, amountofparking, datapayment,status, billid, billpayment });

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        tbdulieu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                e.consume();
                row = tbdulieu.getSelectedRow();
                click0 = tbdulieu.getValueAt(row, 0).toString();
                click1 = tbdulieu.getValueAt(row, 1).toString();
                click2 = tbdulieu.getValueAt(row, 2).toString();               
                click3 = tbdulieu.getValueAt(row, 3).toString();
                click4 = tbdulieu.getValueAt(row, 4).toString();
                click5 = tbdulieu.getValueAt(row, 5).toString();
                click6 = tbdulieu.getValueAt(row, 6).toString();
                click7 = tbdulieu.getValueAt(row, 7).toString();
                click8 = tbdulieu.getValueAt(row, 8).toString();
                click9 = tbdulieu.getValueAt(row, 9).toString();
                click10 = tbdulieu.getValueAt(row, 10).toString();
                click11 = tbdulieu.getValueAt(row, 11).toString();
                click12 = tbdulieu.getValueAt(row, 12).toString();
                click13 = tbdulieu.getValueAt(row, 13).toString();
                click14 = tbdulieu.getValueAt(row, 14).toString();
                click15 = tbdulieu.getValueAt(row, 15).toString();
                
                a0 = click0;
                a1 = click1;
                a2 = click2;
                a3 = click3;
                a4 = click4;
                a5 = click5;
                a6 = click6;
                a7 = click7;
                a8 = click8;
                a9 = click9;
                a10 = click10;
                a11 = click11;
                a12 = click12;
                a13 = click13;
                a14 = click14;
                a15 = click15;
            }
        });
        jScrollPane1.setViewportView(tbdulieu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(lbcompanyname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btsubmit)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtcompanyname, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43)
                                .addComponent(lbstatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtstatus, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(133, 133, 133)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(257, Short.MAX_VALUE)
                .addComponent(btupdatebill)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btpayment)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btexit, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(135, 135, 135))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbcompanyname)
                    .addComponent(txtcompanyname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbstatus)
                    .addComponent(txtstatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btsubmit)
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btpayment)
                    .addComponent(btexit)
                    .addComponent(btupdatebill))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Payment dialog = new Payment(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    class MyItemListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton == false)
                return;
            boolean checked = e.getStateChange() == ItemEvent.SELECTED;
            for (int x = 0, y = tbdulieu.getRowCount(); x < y; x++) {
                tbdulieu.setValueAt(new Boolean(checked), x, 0);
            }
        }
    }

    public static String getA0() {
        return a0;
    }

    public static void setA0(String a0) {
        Payment.a0 = a0;
    }

    public static String getA13() {
        return a13;
    }

    public static void setA13(String a13) {
        Payment.a13 = a13;
    }

    public static String getA14() {
        return a14;
    }

    public static void setA14(String a14) {
        Payment.a14 = a14;
    }
    public String ConverDateTime(String oldday)
    {
        String newday = null;
        try
        {
            
            SimpleDateFormat formatter, FORMATTER;     
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");  
            String oldDate = oldday;  
            Date date;
            date = formatter.parse(oldDate.substring(0, 21));
            FORMATTER = new SimpleDateFormat("dd/MM/yyyy");     
            newday = FORMATTER.format(date);
        }catch(ParseException e)
        {
            e.printStackTrace();
        }     
        
        return newday;
    }
}
